package com.cinc.ebc.sd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cinc.ebc.sd.constant.SdConstant;
import com.cinc.ebc.sd.entity.SdSalesOrder;
import com.cinc.ebc.sd.entity.SdSalesOrderDetail;
import com.cinc.ebc.sd.mapper.SdContractDetailMapper;
import com.cinc.ebc.sd.mapper.SdContractMapper;
import com.cinc.ebc.sd.mapper.SdSalesOrderDetailMapper;
import com.cinc.ebc.sd.mapper.SdSalesOrderMapper;
import com.cinc.ebc.sd.model.*;
import com.cinc.ebc.sd.service.ISdContractService;
import com.cinc.ebc.sd.service.ISdSalesOrderService;
import com.cinc.ebc.sd.vo.SdRptSalesOrderStatPage;
import com.cinc.ebc.sd.vo.SdSalesOrderDetailPage;
import org.jeecg.common.util.oConvertUtils;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.Arrays;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 销售订单
 * @Author: jeecg-boot
 * @Date:   2022-09-25
 * @Version: V1.0
 */
@Service
public class SdSalesOrderServiceImpl extends ServiceImpl<SdSalesOrderMapper, SdSalesOrder> implements ISdSalesOrderService {

	@Autowired
	private SdSalesOrderMapper sdSalesOrderMapper;
	@Autowired
	private SdSalesOrderDetailMapper sdSalesOrderDetailMapper;
	@Autowired
	private ISdContractService sdContractService;

	@Override
	@Transactional
	public void saveMain(SdSalesOrder sdSalesOrder, List<SdSalesOrderDetail> sdSalesOrderDetailList) {
		sdSalesOrderMapper.insert(sdSalesOrder);
		if(sdSalesOrderDetailList!=null && sdSalesOrderDetailList.size()>0) {
			for(SdSalesOrderDetail entity:sdSalesOrderDetailList) {
				//外键设置
				entity.setMainId(sdSalesOrder.getId());
				sdSalesOrderDetailMapper.insert(entity);
			}
		}
		// 更新合同
		if(oConvertUtils.isNotEmpty(sdSalesOrder.getContractId())){
			sdContractService.autoUpdateContract(sdSalesOrder.getContractId());
		}
	}

	@Override
	@Transactional
	public void updateMain(SdSalesOrder sdSalesOrder,List<SdSalesOrderDetail> sdSalesOrderDetailList) {
		SdSalesOrder salesOrderResponse = sdSalesOrderMapper.selectById(sdSalesOrder.getId());
		if(!"01".equals(salesOrderResponse.getStatus())){
			throw new RuntimeException("当前状态不能修改");
		}
		sdSalesOrderMapper.updateById(sdSalesOrder);

		//1.先删除子表数据
		sdSalesOrderDetailMapper.deleteByMainId(sdSalesOrder.getId());

		//2.子表数据重新插入
		if(sdSalesOrderDetailList!=null && sdSalesOrderDetailList.size()>0) {
			for(SdSalesOrderDetail entity:sdSalesOrderDetailList) {
				//外键设置
				entity.setMainId(sdSalesOrder.getId());
				sdSalesOrderDetailMapper.insert(entity);
			}
		}

		// 更新合同
		if(oConvertUtils.isNotEmpty(sdSalesOrder.getContractId())){
			sdContractService.autoUpdateContract(sdSalesOrder.getContractId());
		}
	}

	@Override
	@Transactional
	public void delMain(String id) {
		SdSalesOrder salesOrderResponse = sdSalesOrderMapper.selectById(id);
		if(!"01".equals(salesOrderResponse.getStatus())){
			throw new RuntimeException("当前状态不能删除");
		}
		sdSalesOrderDetailMapper.deleteByMainId(id);
		sdSalesOrderMapper.deleteById(id);

		// 更新合同
		if(oConvertUtils.isNotEmpty(salesOrderResponse.getContractId())){
			sdContractService.autoUpdateContract(salesOrderResponse.getContractId());
		}
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			SdSalesOrder salesOrderResponse = sdSalesOrderMapper.selectById(id);
			if(!"01".equals(salesOrderResponse.getStatus())){
				throw new RuntimeException("当前状态不能删除");
			}
			sdSalesOrderDetailMapper.deleteByMainId(id.toString());
			sdSalesOrderMapper.deleteById(id);

			// 更新合同
			if(oConvertUtils.isNotEmpty(salesOrderResponse.getContractId())){
				sdContractService.autoUpdateContract(salesOrderResponse.getContractId());
			}
		}
	}

	@Transactional
	@Override
	public void autoUpdateSalesOrder(String id){
		SdSalesOrder sdSalesOrder = sdSalesOrderMapper.selectById(id);
		List<String> statusList = Arrays.asList(SdConstant.SAL_ORDER_STATUS03, SdConstant.SAL_ORDER_STATUS99);
		if(statusList.contains(sdSalesOrder.getStatus())){
			// 完成/关闭状态的单据，不处理
			return;
		}
		// 更新明细
		List<SdSalesOrderDetail> sdSalesOrderDetailList = sdSalesOrderDetailMapper.selectByMainId(id);
		if(oConvertUtils.listIsNotEmpty(sdSalesOrderDetailList)){
			for(SdSalesOrderDetail orderDetail : sdSalesOrderDetailList){
				// 数量更新
				sdSalesOrderDetailMapper.autoUpdateQtyInfo(orderDetail.getId());
				// 状态更新
				sdSalesOrderDetailMapper.autoUpdateStatus(orderDetail.getId());
			}
		}
		// 更新主表
		sdSalesOrderMapper.autoUpdateStatus(id);
	}

	@Override
	public IPage<SdRptSalesOrderMaterialStatModel> queryRptMaterialStatList(Page<SdRptSalesOrderMaterialStatModel> page, SdRptSalesOrderStatPage sdRptSalesOrderStatPage) {
		return page.setRecords(sdSalesOrderMapper.selectRptMaterialStatList(page, sdRptSalesOrderStatPage));
	}

	@Override
	public SdRptSalesOrderMaterialStatTotalModel queryRptMaterialStatTotal(SdRptSalesOrderStatPage sdRptSalesOrderStatPage) {
		return sdSalesOrderMapper.selectRptMaterialStatTotal(sdRptSalesOrderStatPage);
	}

	@Override
	public IPage<SdRptSalesOrderCustomerStatModel> queryRptCustomerStatList(Page<SdRptSalesOrderCustomerStatModel> page, SdRptSalesOrderStatPage sdRptSalesOrderStatPage) {
		return page.setRecords(sdSalesOrderMapper.selectRptCustomerStatList(page, sdRptSalesOrderStatPage));
	}

	@Override
	public SdRptSalesOrderCustomerStatTotalModel queryRptCustomerStatTotal(SdRptSalesOrderStatPage sdRptSalesOrderStatPage) {
		return sdSalesOrderMapper.selectRptCustomerStatTotal(sdRptSalesOrderStatPage);
	}

	@Override
	public void queryCustomerMaterialStatPageList(Page<SdSalesOrderCustomerMaterialModel> page, SdSalesOrderDetailPage detailPage) {

		page.setRecords(sdSalesOrderMapper.selectCustomerMaterialStatList(page, detailPage, null));
	}

}
